package net.begincode.SSO.util;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Properties;
import java.util.Set;

import redis.clients.jedis.Jedis;

/**
 * Jedis工具
 * @author 遗忘
 * @date 2017.5.7
 * 
 */
public class JedisUtil {
	private static Jedis jedis;
	static{
		try {
			Properties properties = new Properties();
			InputStream input = JedisUtil.class.getResourceAsStream("/jedis.properties");
			properties.load(input);
			input.close();
			jedis = new Jedis(properties.getProperty("host"),Integer.parseInt(properties.getProperty("port")));
			try {
				jedis.configGet("requirepass");
			} catch (Exception e) {
				jedis.auth(properties.getProperty("password"));
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	public static Jedis getJedis(){
		return jedis;
	}
	
	public static void setList(String key,List<String> list,int deadline){
		for(String value:list){
			jedis.lpush(key, value);
		}
		jedis.expire(key, deadline);
	}
	
	public static void setList(String key,String value,int deadline){
		jedis.lpush(key, value);
		jedis.expire(key, deadline);
	}
	
	public static List<String> getList(String key){
		return jedis.lrange(key, 0, 20);
	}
	
	public static boolean existsKey(String key){
		return jedis.exists(key);
	}
	
	public static void removeKey(String key){
		jedis.del(key);
	}
	public static void close(){
		if(jedis!=null){
			jedis.close();
		}
	}
	
	public static void delListValue(String key,String value){
		jedis.lrem(key, 0, value);
	}
	
	public static void setSet(String key,Set<String> set,int deadline){
		for(String value:set){
			jedis.sadd(key, value);
		}
		jedis.expire(key, deadline);
	}
	
	public static void setSet(String key,String value,int deadline){
		jedis.sadd(key, value);
		jedis.expire(key, deadline);
	}
	
	public static Set<String> getSet(String key){
		return jedis.smembers(key);
	}
	
	public static void delSetValue(String key,String value){
		jedis.srem(key,value);
	}
	
}
